package cn.yiya.shiji.activity;
/**
 * 消息中心界面
 */

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;

import java.util.ArrayList;

import cn.yiya.shiji.R;
import cn.yiya.shiji.adapter.MessageCentListAdapter;
import cn.yiya.shiji.business.ApiRequest;
import cn.yiya.shiji.business.HttpMessage;
import cn.yiya.shiji.business.MapRequest;
import cn.yiya.shiji.business.MsgCallBack;
import cn.yiya.shiji.business.RetrofitRequest;
import cn.yiya.shiji.entity.NotifyDetailCount;
import cn.yiya.shiji.entity.NotifyListItem;
import cn.yiya.shiji.entity.NotifyListObject;
import cn.yiya.shiji.utils.SimpleUtils;
import cn.yiya.shiji.views.MySwipeMenuListview;


public class MessageCenterActivity extends BaseAppCompatActivity implements View.OnClickListener {
    private Handler mHandler;
    private ScrollView mScrollView;
    private RelativeLayout rlytLogistics;
    private RelativeLayout rlytCommunity;
    private RelativeLayout rlytOther;
    private TextView tvLogistics;
    private TextView tvCommunity;
    private TextView tvOther;
    private LinearLayout llytLogistics;
    private LinearLayout llytCommunity;
    private MySwipeMenuListview mLogisticsListView;
    private MySwipeMenuListview mCommunityListView;
    private MySwipeMenuListview mOtherListView;

    private LinearLayout mCommunityFooterView;
    private LinearLayout mLogisticsFooterView;
    private LinearLayout llyCommunityLoardMore;
    private ProgressBar mCommunityProgressBar;
    private TextView tvCommunityLoard;
    private LinearLayout llyLogisticsLoardMore;
    private ProgressBar mLogisticsProgressBar;
    private TextView tvLogisticsLoard;

    private ImageView ivBack;
    private TextView tvtitle;

    public static final int LOGISTICS_TYPE = 1;
    public static final int COMMUNITY_TYPE = 2;

    private ArrayList<NotifyListItem> mLogisticsLists;
    private ArrayList<NotifyListItem> mCommunityLists;
    private MessageCentListAdapter mAdapterLogistics;
    private MessageCentListAdapter mAdapterCommunity;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_center);
        mHandler = new Handler();
        initViews();
        initEvents();
        init();
    }

    @Override
    protected void initViews() {
        LayoutInflater inflater = LayoutInflater.from(this);
        mScrollView = (ScrollView) findViewById(R.id.mine_scroll);
        mScrollView.smoothScrollTo(0,0);
        mCommunityFooterView = (LinearLayout)inflater.inflate(R.layout.mine_footer_view, null);
        mLogisticsFooterView = (LinearLayout)inflater.inflate(R.layout.mine_footer_view, null);
        llyCommunityLoardMore = (LinearLayout)mCommunityFooterView.findViewById(R.id.lly_loard_more);
        mCommunityProgressBar = (ProgressBar)mCommunityFooterView.findViewById(R.id.pro_lord_more);
        tvCommunityLoard = (TextView)mCommunityFooterView.findViewById(R.id.msg_loard_more);
        llyLogisticsLoardMore = (LinearLayout)mLogisticsFooterView.findViewById(R.id.lly_loard_more);
        mLogisticsProgressBar = (ProgressBar)mLogisticsFooterView.findViewById(R.id.pro_lord_more);
        tvLogisticsLoard = (TextView)mLogisticsFooterView.findViewById(R.id.msg_loard_more);

        rlytLogistics = (RelativeLayout)findViewById(R.id.msg_logistics);
        rlytCommunity = (RelativeLayout)findViewById(R.id.msg_community);
        rlytOther = (RelativeLayout)findViewById(R.id.msg_other);

        llytLogistics = (LinearLayout) findViewById(R.id.llyt_logistics);
        mLogisticsListView = (MySwipeMenuListview)findViewById(R.id.logistics_list);
        llytCommunity = (LinearLayout) findViewById(R.id.llyt_community);
        mCommunityListView = (MySwipeMenuListview)findViewById(R.id.community_list);
        mOtherListView = (MySwipeMenuListview)findViewById(R.id.other_list);

        mLogisticsListView.addFooterView(mLogisticsFooterView);
        mCommunityListView.addFooterView(mCommunityFooterView);

        SwipeMenuCreator creator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu swipeMenu) {
                SwipeMenuItem deleteItem = new SwipeMenuItem(
                        getApplicationContext());
                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xEb, 0x64,
                        0x67)));
                deleteItem.setWidth(SimpleUtils.px2dp(MessageCenterActivity.this, SimpleUtils.getScreenHeight(MessageCenterActivity.this)/4));
                deleteItem.setIcon(R.mipmap.del_icon_normal);
                swipeMenu.addMenuItem(deleteItem);
            }
        };

        mLogisticsListView.setMenuCreator(creator);
        mCommunityListView.setMenuCreator(creator);
        mOtherListView.setMenuCreator(creator);

        tvLogistics = (TextView)findViewById(R.id.tv_logistics_num);
        tvCommunity = (TextView)findViewById(R.id.tv_community_num);
        tvOther = (TextView)findViewById(R.id.other_count);

        ivBack = (ImageView)findViewById(R.id.title_back);
        tvtitle = (TextView)findViewById(R.id.title_txt);
        tvtitle.setText("消息中心");
        findViewById(R.id.title_right).setVisibility(View.GONE);
    }

    @Override
    protected  void initEvents(){
        ivBack.setOnClickListener(this);
        rlytLogistics.setOnClickListener(this);
        rlytCommunity.setOnClickListener(this);
        rlytOther.setOnClickListener(this);
        mLogisticsListView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu swipeMenu, int i1) {
                deleteMsg(mLogisticsLists.get(position).getId(), position, LOGISTICS_TYPE);
                return false;
            }
        });
        mLogisticsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == mLogisticsLists.size()) {
                    mLogisticsProgressBar.setVisibility(View.VISIBLE);
                    tvLogisticsLoard.setText("加载中...");
                    new RetrofitRequest<NotifyListObject>(ApiRequest.getApiShiji().getNotifyList(
                            MapRequest.setMapMatchRecommendList(mLogisticsLists.size(), 20, String.valueOf(1))
                    )).handRequest(new MsgCallBack() {
                        @Override
                        public void onResult(HttpMessage msg) {
                            if (msg.isSuccess()) {
                                NotifyListObject info = (NotifyListObject) msg.obj;
                                if (info.list != null && info.list.size() > 0) {
                                    mLogisticsLists.addAll(info.list);
                                    mAdapterLogistics.notifyDataSetChanged();
                                    mLogisticsFooterView.setVisibility(View.VISIBLE);
                                    mLogisticsProgressBar.setVisibility(View.GONE);
                                    tvLogisticsLoard.setText("加载更多");
                                }else {
                                    mLogisticsFooterView.setVisibility(View.GONE);
                                    mLogisticsProgressBar.setVisibility(View.GONE);
                                    tvLogisticsLoard.setVisibility(View.GONE);
                                }
                            } else {
                                showTips(msg.message);
                                mLogisticsFooterView.setVisibility(View.VISIBLE);
                                mLogisticsProgressBar.setVisibility(View.GONE);
                                tvLogisticsLoard.setText("加载更多");
                            }
                        }
                    });
                }
            }
        });

        mCommunityListView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu swipeMenu, int i1) {
                deleteMsg(mCommunityLists.get(position).getId(), position, COMMUNITY_TYPE);
                return false;
            }
        });

        mCommunityListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == mCommunityLists.size()) {
                    mCommunityProgressBar.setVisibility(View.VISIBLE);
                    tvCommunityLoard.setText("加载中...");
                    new RetrofitRequest<NotifyListObject>(ApiRequest.getApiShiji().getNotifyList(MapRequest
                    .setMapMatchRecommendList(mCommunityLists.size(), 20, String.valueOf(2)))).handRequest(
                            new MsgCallBack() {
                                @Override
                                public void onResult(HttpMessage msg) {
                                    hidePreDialog();
                                    if (msg.isSuccess()) {
                                        NotifyListObject info = (NotifyListObject) msg.obj;
                                        if (info.list != null && info.list.size() > 0) {
                                            mCommunityLists.addAll(info.list);
                                            mAdapterCommunity.notifyDataSetChanged();
                                            mCommunityFooterView.setVisibility(View.VISIBLE);
                                            mCommunityProgressBar.setVisibility(View.GONE);
                                            tvCommunityLoard.setText("加载更多");
                                        }else {
                                            mCommunityFooterView.setVisibility(View.GONE);
                                            mCommunityProgressBar.setVisibility(View.GONE);
                                            tvCommunityLoard.setVisibility(View.GONE);
                                        }
                                    } else {
                                        showTips(msg.message);
                                        mCommunityFooterView.setVisibility(View.VISIBLE);
                                        mCommunityProgressBar.setVisibility(View.GONE);
                                        tvCommunityLoard.setText("加载更多");
                                    }
                                }
                            }
                    );
                }
            }
        });

        //解决listView不能滑动问题
        mLogisticsListView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    mScrollView.requestDisallowInterceptTouchEvent(true);
                } else {
                    mScrollView.requestDisallowInterceptTouchEvent(true);
                }
                return false;
            }
        });

        mCommunityListView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    mScrollView.requestDisallowInterceptTouchEvent(true);
                } else {
                    mScrollView.requestDisallowInterceptTouchEvent(true);
                }
                return false;
            }
        });
    }

    @Override
    protected void init() {
        getGroupData();
    }

    private void getGroupData() {
        showPreDialog("正在加载");
        new RetrofitRequest<NotifyDetailCount>(ApiRequest.getApiShiji().getNotifyDetailCount()).handRequest(
                new MsgCallBack() {
                    @Override
                    public void onResult(HttpMessage msg) {
                        hidePreDialog();
                        if (msg.isSuccess()) {
                            NotifyDetailCount info = (NotifyDetailCount) msg.obj;
                            if (info.getLogistics_count() > 0) {
                                if(info.getLogistics_count() > 99){
                                    tvLogistics.setVisibility(View.VISIBLE);
                                    tvLogistics.setText("99+");
                                }else {
                                    tvLogistics.setVisibility(View.VISIBLE);
                                    tvLogistics.setText("" + info.getLogistics_count());
                                }

                            } else {
                                tvLogistics.setVisibility(View.INVISIBLE);
                            }
                            if (info.getSns_count() > 0) {
                                if(info.getSns_count() > 99){
                                    tvCommunity.setVisibility(View.VISIBLE);
                                    tvCommunity.setText("99+");
                                }else {
                                    tvCommunity.setVisibility(View.VISIBLE);
                                    tvCommunity.setText("" + info.getSns_count());
                                }

                            } else {
                                tvCommunity.setVisibility(View.INVISIBLE);
                            }
                        }
                    }
                }
        );
    }

    private void upDateMessageNumber(final int type){
        new RetrofitRequest<NotifyDetailCount>(ApiRequest.getApiShiji().getNotifyDetailCount()).handRequest(
                new MsgCallBack() {
            @Override
            public void onResult(HttpMessage msg) {
                if(msg.isSuccess()){
                    NotifyDetailCount info = (NotifyDetailCount)msg.obj;
                    if(type == LOGISTICS_TYPE){
                        if (info.getLogistics_count() > 0) {
                            if(info.getLogistics_count() > 99){
                                tvLogistics.setText("99+");
                            }else {
                                tvLogistics.setText("" + info.getLogistics_count());
                            }
                        } else {
                            tvLogistics.setVisibility(View.INVISIBLE);
                        }
                    }else {
                        if (info.getSns_count() > 0) {
                            if(info.getSns_count() > 99){
                                tvCommunity.setText("99+");
                            }else {
                                tvCommunity.setText("" + info.getSns_count());
                            }
                        } else {
                            tvCommunity.setVisibility(View.INVISIBLE);
                        }
                    }

                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.title_back:
                setResult(RESULT_OK);
                finish();
                break;
            case R.id.msg_logistics:
                if (llytLogistics.getVisibility() == View.VISIBLE) {
                    llytLogistics.setVisibility(View.GONE);
                    upDateMessageNumber(LOGISTICS_TYPE);
                } else {
                    llytLogistics.setVisibility(View.VISIBLE);
                    getListData(LOGISTICS_TYPE, rlytLogistics);
                }
                break;
            case R.id.msg_community:
                if (llytCommunity.getVisibility() == View.VISIBLE) {
                    llytCommunity.setVisibility(View.GONE);
                    upDateMessageNumber(COMMUNITY_TYPE);
                } else {
                    llytCommunity.setVisibility(View.VISIBLE);
                    getListData(COMMUNITY_TYPE, rlytCommunity);
                }

                break;
            case R.id.msg_other:
                break;
        }
    }

    private void getListData(final int type, View view) {
        if (view.getId() == R.id.msg_community) {
            if (mCommunityLists != null) {
                return;
            }
        } else {
            if (mLogisticsLists != null) {
                return;
            }
        }

        showPreDialog("加载中");
        new RetrofitRequest<NotifyListObject>(ApiRequest.getApiShiji().getNotifyList(MapRequest
        .setMapMatchRecommendList(0, 20, String.valueOf(type)))).handRequest(new MsgCallBack() {
            @Override
            public void onResult(HttpMessage msg) {
                hidePreDialog();
                if(msg.isSuccess()) {
                    NotifyListObject info = (NotifyListObject) msg.obj;

                    if (type == LOGISTICS_TYPE) {
                        if(info.list != null && info.list.size()>0){
                            mLogisticsLists = info.list;
                            llyLogisticsLoardMore.setVisibility(View.VISIBLE);
                            mLogisticsProgressBar.setVisibility(View.GONE);
                            tvLogisticsLoard.setText("加载更多");
                            mAdapterLogistics = new MessageCentListAdapter(MessageCenterActivity.this, mLogisticsLists, type);
                            mLogisticsListView.setAdapter(mAdapterLogistics);
                        }else {
                            llyLogisticsLoardMore.setVisibility(View.GONE);
                        }
                    } else {
                        if(info.list != null && info.list.size()>0){
                            mCommunityLists = info.list;
                            llyCommunityLoardMore.setVisibility(View.VISIBLE);
                            mCommunityProgressBar.setVisibility(View.GONE);
                            tvCommunityLoard.setText("加载更多");
                            mAdapterCommunity = new MessageCentListAdapter(MessageCenterActivity.this, mCommunityLists, type);
                            mCommunityListView.setAdapter(mAdapterCommunity);
                        }else {
                            llyCommunityLoardMore.setVisibility(View.GONE);
                        }
                    }
                }
            }
        });
    }

    private void deleteMsg(int id, final int positon, final int type) {
        new RetrofitRequest<>(ApiRequest.getApiShiji().deleteNotify(String.valueOf(id))).handRequest(new MsgCallBack() {
            @Override
            public void onResult(HttpMessage msg) {
                if (msg.isSuccess()) {
                    showTips("删除成功");
                    if(type == COMMUNITY_TYPE){
                        mCommunityLists.remove(positon);
                        mAdapterCommunity.notifyDataSetChanged();
                    }else {
                        mLogisticsLists.remove(positon);
                        mAdapterLogistics.notifyDataSetChanged();
                    }

                }
            }
        });
    }

    @Override
    public void onBackPressed() {
        setResult(RESULT_OK);
        finish();
    }
}
